package esiag.pds.PoolThread;

import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import esiag.pds.contextManagement.Context;
import esiag.pds.contextManagement.ContextManager;


public class MyThreadPool {

//	private static final long serialVersionUID = 1L;
	
	RejectedExecutionHandlerImpl rehi;
	ThreadFactory threadFactory;
	ExecutorService  executorPool;
	Future<Context> future;
	
	public MyThreadPool(int min,int max,int aliveTime, int blockingQueue) {
		// TODO Auto-generated constructor stub
		rehi=new RejectedExecutionHandlerImpl();
		threadFactory = Executors.defaultThreadFactory();
		executorPool = new ThreadPoolExecutor(min,max,aliveTime,
				TimeUnit.MILLISECONDS, 
				new ArrayBlockingQueue<Runnable>(blockingQueue),
				threadFactory, rehi);
	}
	
	public Context doWork(Context c){
		future=executorPool.submit(new ReplicationCallable(c));
		try {
			return future.get();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		
	}
}
